package com.lb.videoplay.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.videoplay.R;
import com.lb.videoplay.utils.CommonUtils;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 登录页面
 */
public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText mEtNumber;
    private ImageView mIvClose;
    private ImageView mIvCloses;
    private EditText mEtPwd;
    private ImageView mTvPrompt;
    private TextView mTvApplyFor;
    private Button mBtLogin;

    private boolean noClose;//是否可见
    private CommonUtils mCommonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initTitle();
        initWidget();
        initEvent();
        mCommonUtils = new CommonUtils(this);
    }

    private void initWidget() {
        mEtNumber = findViewById(R.id.ed_number);
        mIvClose = findViewById(R.id.iv_close);
        mIvCloses = findViewById(R.id.iv_closes);
        mEtPwd = findViewById(R.id.ed_pwd);
        mTvPrompt = findViewById(R.id.tv_prompt);
        mTvApplyFor = findViewById(R.id.tv_apply_for);
        mBtLogin = findViewById(R.id.bt_login);
    }

    private void initEvent() {
        mBtLogin.setOnClickListener(this);
        mIvClose.setOnClickListener(this);
        mIvCloses.setOnClickListener(this);
        mTvApplyFor.setOnClickListener(this);
        mTvPrompt.setOnClickListener(this);
        //监听手机号码
        mEtNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    if (mEtPwd.length() > 0) {
                        mBtLogin.setBackgroundResource(R.drawable.button_login);
                        mBtLogin.setEnabled(true);
                    }
                    mIvClose.setVisibility(View.VISIBLE);
                    mIvClose.setEnabled(true);
                } else {
                    mBtLogin.setBackgroundResource(R.drawable.bt_width_select);
                    mBtLogin.setEnabled(false);
                    mIvClose.setVisibility(View.GONE);
                    mIvClose.setEnabled(false);
                }
            }
        });
        //监听密码框
        mEtPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    if (mEtNumber.length() > 0) {
                        mBtLogin.setBackgroundResource(R.drawable.button_login);
                        mBtLogin.setEnabled(true);
                    }
                    mIvCloses.setVisibility(View.VISIBLE);
                    mIvCloses.setEnabled(true);
                } else {
                    mBtLogin.setBackgroundResource(R.drawable.bt_width_select);
                    mBtLogin.setEnabled(false);
                    mIvCloses.setVisibility(View.GONE);
                    mIvCloses.setEnabled(false);
                }
            }
        });
    }

    //初始化标题
    public void initTitle() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                );
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            //window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login: //登录
                login(mEtNumber.getText().toString(), mEtPwd.getText().toString());
                break;
            case R.id.iv_close:  //清空
                mEtNumber.setText("");
                break;
            case R.id.iv_closes:
                mEtPwd.setText("");
                break;
            case R.id.tv_apply_for: //跳转注册
                ActivityUtils.startActivity(RegisterActivity.class);
                break;
            case R.id.tv_prompt://密码是否可见
                if (noClose) {
                    mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码不可见
                    mTvPrompt.setImageDrawable(getResources().getDrawable(R.drawable.log_password_shut));
                    noClose = false;
                    mEtPwd.setSelection(mEtPwd.getText().length());
                } else {
                    mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//设置密码可见
                    mTvPrompt.setImageDrawable(getResources().getDrawable(R.drawable.log_password_down));
                    noClose = true;
                    mEtPwd.setSelection(mEtPwd.getText().length());
                }
                break;
        }
    }

    private void login(String userPhone, String pwd) {
        if (ObjectUtils.isEmpty(userPhone)) {
            ToastUtils.showShort("请输入账号");
            return;
        }
        if (!mCommonUtils.isChinaPhoneLegal(userPhone)) {
            ToastUtils.showShort("您的手机号码格式有误");
            return;
        }
        if (ObjectUtils.isEmpty(pwd)) {
            ToastUtils.showShort("请输入密码");
            return;
        }
        if (pwd.length() < 6) {
            ToastUtils.showShort("账号密码输入有误");
            return;
        }
        if (!ObjectUtils.isEmpty(SPUtils.getInstance().getString("phone"))) {
            if (userPhone.equals(SPUtils.getInstance().getString("phone")) && pwd.equals(SPUtils.getInstance().getString("pwd"))) {
                mCommonUtils.showLoadDialog("登录");
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        mCommonUtils.dissDialog();
                        SPUtils.getInstance().put("isLogin", true);
                        ActivityUtils.startActivity(MainActivity.class);
                        finish();
                    }
                }, 2000);
            }
        } else {
            ToastUtils.showShort("请先注册");
        }
    }
}
